import { createRouter, createWebHistory } from 'vue-router'
import { useTokenStore } from '@/stores/token.js'
import useUserInfoStore from '@/stores/userInfo.js'

import IndexView from '@/views/index/IndexVue.vue'
import LayoutView from '@/views/layout/LayoutVue.vue'
import LoginView from '@/views/login/LoginVue.vue'
import DashboardView from '@/views/dashboard/DashboardVue.vue'
import DemoView from '@/views/Z-Test/Demo.vue'


// 商品相关
import ProductListView from '@/views/product/ProductListVue.vue'
import ProductAddView from '@/views/product/ProductAddVue.vue'
import ProductCategoryView from '@/views/product/ProductCategoryVue.vue'
import ProductTypeView from '@/views/product/ProductTypeVue.vue'
import ProductBrandView from '@/views/product/ProductBrandVue.vue'


// 订单相关
import OrderListView from '@/views/order/OrderListVue.vue'
import OrderRefundView from '@/views/order/OrderRefundVue.vue'
import OrderReasonView from '@/views/order/OrderReasonVue.vue'
import OrderInfoView from '@/views/order/OrderInfoVue.vue'
import OrderRefundInfoView from '@/views/order/OrderRefundInfoVue.vue'

// 营销相关
import MarketingSeckillView from '@/views/marketing/MarketingSeckillVue.vue'
import MarketingCouponView from '@/views/marketing/MarketingCouponVue.vue'
import MarketingBrandView from '@/views/marketing/MarketingBrandVue.vue'
import MarketingNewView from '@/views/marketing/MarketingNewVue.vue'
import MarketingPopularView from '@/views/marketing/MarketingPopularVue.vue'
import MarketingTopicView from '@/views/marketing/MarketingTopicVue.vue'
import MarketingAdvertisementView from '@/views/marketing/MarketingAdvertisementVue.vue'

// 权限相关
import PermissionUserView from '@/views/permission/PermissionUserVue.vue'
import PermissionRoleView from '@/views/permission/PermissionRoleVue.vue'
import PermissionMenuView from '@/views/permission/PermissionMenuVue.vue'
import PermissionResourceView from '@/views/permission/PermissionResourceVue.vue'

// 个人中心
import UserAvatar from '@/views/user/UserAvatarVue.vue'
import UserProfile from '@/views/user/UserProfileVue.vue'
import UserPassword from '@/views/user/UserResetPasswordVue.vue'
// 错误页面
import NotFound from '@/views/error/404.vue'
import ServerError from '@/views/error/500.vue'
// 主页测试
import Home from '@/views/mallIndex/Home.vue'
import MightLike from '@/views/mallIndex/MightLike.vue'
import Featured from '@/views/mallIndex/Featured.vue'
import NewProducts from '@/views/mallIndex/NewProducts.vue'
import ProductDetail from '@/views/mallIndex/ProductDetail.vue'
import Category from '@/views/mallIndex/Category.vue'
import Cart from '@/views/mallIndex/Cart.vue'
import UserView from '@/views/mallIndex/User.vue'
import SettingsView from '@/views/mallIndex/SettingsView.vue'
import SearchView from '@/views/mallIndex/SearchView.vue'
import FavoritesView from '@/views/mallIndex/FavoritesView.vue'
import HistoryView from '@/views/mallIndex/HistoryView.vue'

const routes = [

    // 登录注册
    { 
        path: '/login', 
        component: LoginView,
        meta: {
            title: '登录',
            hidden: true
        }
    },
    // 测试页面
    { 
        path: '/demo', 
        component: DemoView,
        meta: {
            title: '演示页面',
            hidden: true
        }
    },
    
    // 前台商城路由
    { 
        path: '/home', 
        component: Home,
        meta: {
            title: '商城首页'
        }
    },
    { 
        path: '/home/category/mightLike', 
        component: MightLike,
        meta: {
            title: '猜你喜欢'
        }
    },
    { 
        path: '/home/category/featured', 
        component: Featured,
        meta: {
            title: '精选商品'
        }
    },
    { 
        path: '/home/category/new', 
        component: NewProducts,
        meta: {
            title: '新品上市'
        }
    },
    { 
        path: '/home/product/:id', 
        component: ProductDetail,
        meta: {
            title: '商品详情'
        }
    },
    { 
        path: '/home/category/:type', 
        component: Category,
        meta: {
            title: '商品分类'
        }
    },
    { 
        path: '/home/cart', 
        component: Cart,
        meta: {
            title: '购物车',
            requiresAuth: true
        }
    },
    { 
        path: '/home/user', 
        component: UserView,
        meta: {
            title: '个人中心',
            requiresAuth: true
        }
    },
    { 
        path: '/home/settings', 
        component: SettingsView,
        meta: {
            title: '设置',
            requiresAuth: true
        }
    },
    { 
        path: '/home/search', 
        component: SearchView,
        meta: {
            title: '搜索'
        }
    },
    { 
        path: '/home/favorites', 
        component: FavoritesView,
        meta: {
            title: '收藏夹',
            requiresAuth: true
        }
    },
    { 
        path: '/home/history', 
        component: HistoryView,
        meta: {
            title: '浏览历史',
            requiresAuth: true
        }
    },
    {
        path: '/home/order/confirm',
        name: 'OrderConfirm',
        component: () => import('@/views/mallIndex/OrderConfirm.vue'),
        meta: {
            title: '确认订单',
            requiresAuth: true
        }
    },
    {
        path: '/home/order/success',
        name: 'OrderSuccess',
        component: () => import('@/views/mallIndex/OrderSuccess.vue'),
        meta: {
            title: '下单成功',
            requiresAuth: true
        }
    },
    {
        path: '/home/order/list',
        component: () => import('@/views/order/OrderListPage.vue')
    },

    // 错误页面
    { 
        path: '/500', 
        component: ServerError,
        meta: {
            title: '服务器错误',
            hidden: true
        }
    },
    { 
        path: '/:pathMatch(.*)*', 
        component: NotFound,
        meta: {
            title: '页面未找到',
            hidden: true
        }
    },
    
    // 后台管理路由
    {
        path: '/admin',
        component: LayoutView,
        meta: {
            title: '后台管理'
        },
        children: [
            { 
                path: 'index', 
                component: IndexView,
                meta: {
                    title: '控制台'
                }
            },
            { 
                path: 'dashboard', 
                component: DashboardView,
                meta: {
                    title: '数据概览'
                }
            },
            {
                path: 'product',
                redirect: 'product/list',
                meta: {
                    title: '商品管理'
                },
                children: [
                    { 
                        path: 'list', 
                        component: ProductListView,
                        meta: {
                            title: '商品列表'
                        }
                    },
                    { 
                        path: 'add', 
                        component: ProductAddView,
                        meta: {
                            title: '添加商品'
                        }
                    },
                    { 
                        path: 'category', 
                        component: ProductCategoryView,
                        meta: {
                            title: '商品分类'
                        }
                    },
                    { 
                        path: 'type', 
                        component: ProductTypeView,
                        meta: {
                            title: '商品类型'
                        }
                    },
                    { 
                        path: 'brand', 
                        component: ProductBrandView,
                        meta: {
                            title: '品牌管理'
                        }
                    }
                ]
            },
            {
                path: 'order',
                redirect: 'order/list',
                meta: {
                    title: '订单管理'
                },
                children: [
                    { 
                        path: 'list', 
                        component: OrderListView,
                        meta: {
                            title: '订单列表'
                        }
                    },
                    { 
                        path: 'refund', 
                        component: OrderRefundView,
                        meta: {
                            title: '退款管理'
                        }
                    },
                    { 
                        path: 'reason', 
                        component: OrderReasonView,
                        meta: {
                            title: '退款原因'
                        }
                    },
                    { 
                        path: 'info/:id', 
                        component: OrderInfoView,
                        meta: {
                            title: '订单详情'
                        }
                    },
                    { 
                        path: 'refund/info/:id', 
                        component: OrderRefundInfoView,
                        meta: {
                            title: '退单详情'
                        }
                    }
                ]
            },
            {
                path: 'marketing',
                redirect: 'marketing/seckill',
                meta: {
                    title: '营销管理'
                },
                children: [
                    { 
                        path: 'seckill', 
                        component: MarketingSeckillView,
                        meta: {
                            title: '秒杀活动'
                        }
                    },
                    { 
                        path: 'coupon', 
                        component: MarketingCouponView,
                        meta: {
                            title: '优惠券'
                        }
                    },
                    { 
                        path: 'brand', 
                        component: MarketingBrandView,
                        meta: {
                            title: '品牌营销'
                        }
                    },
                    { 
                        path: 'new', 
                        component: MarketingNewView,
                        meta: {
                            title: '新品营销'
                        }
                    },
                    { 
                        path: 'popular', 
                        component: MarketingPopularView,
                        meta: {
                            title: '热门商品'
                        }
                    },
                    { 
                        path: 'topic', 
                        component: MarketingTopicView,
                        meta: {
                            title: '专题活动'
                        }
                    },
                    { 
                        path: 'advertisement', 
                        component: MarketingAdvertisementView,
                        meta: {
                            title: '广告管理'
                        }
                    }
                ]
            },
            {
                path: 'permission',
                redirect: 'permission/user',
                meta: {
                    title: '权限管理'
                },
                children: [
                    { 
                        path: 'user', 
                        component: PermissionUserView,
                        meta: {
                            title: '用户管理'
                        }
                    },
                    { 
                        path: 'role', 
                        component: PermissionRoleView,
                        meta: {
                            title: '角色管理'
                        }
                    },
                    { 
                        path: 'menu', 
                        component: PermissionMenuView,
                        meta: {
                            title: '菜单管理'
                        }
                    },
                    { 
                        path: 'resource', 
                        component: PermissionResourceView,
                        meta: {
                            title: '资源管理'
                        }
                    }
                ]
            },
            {
                path: 'user',
                redirect: 'user/avatar',
                meta: {
                    title: '个人中心'
                },
                children: [
                    { 
                        path: 'avatar', 
                        component: UserAvatar,
                        meta: {
                            title: '修改头像'
                        }
                    },
                    { 
                        path: 'profile', 
                        component: UserProfile,
                        meta: {
                            title: '个人资料'
                        }
                    },
                    { 
                        path: 'resetPassword', 
                        component: UserPassword,
                        meta: {
                            title: '修改密码'
                        }
                    }
                ]
            }
        ]
    }
]
// 创建路由实例
const router = createRouter({
    history: createWebHistory(),
    routes,
    scrollBehavior(to, from, savedPosition) {
        // 始终滚动到顶部
        return { top: 0, behavior: 'smooth' }
    },
})

// 全局后置守卫：确保页面滚动正常
router.afterEach(() => {
    document.body.style.overflow = '';
});
// 路由守卫
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = 'Pardo - ' + to.meta.title
    } else {
        document.title = 'Pardo-Mall'
    }
    
    // 检查是否需要登录
    if (to.meta.requiresAuth) {
        const tokenStore = useTokenStore()
        const userInfoStore = useUserInfoStore()
        
        // 检查是否已登录（有token且有用户信息）
        if (!tokenStore.token || !userInfoStore.info.nickName) {
            // 未登录，显示提示并跳转到登录页
            import('element-plus').then(({ ElMessage }) => {
                ElMessage.error('请先登录后再访问此页面')
            })
            next('/login')
            return
        }
    }
    
    next()
})
// 导出路由实例
export default router